var done = {};

$('document').ready(
    function() {
        $('a[href*="#"]').click(
            function(event) {
                loadPage(getLocationFromUrl(event.currentTarget.href));
            }
        );
        loadPage(getCurrentLocation());
    }
)

var getCurrentLocation = function() {
    return getLocationFromUrl('' + window.location);
}

var getLocationFromUrl = function(url) {
    if (url.indexOf('#') >= 0) {
        return url.substring(url.indexOf('#') + 1);
    }
    return "home";
}

var loadPage = function(pageName) {
    if (pageName != undefined && pageName.trim() != '') {
        var element = $('#location-' + pageName);
        if (element.length != 0) {
            $('#content').html(element.html());
        }
        if (pageName == 'reset') {
            pingUrl("/rest/prolog/reasoning/reset");
            for (var value in done) {
                value = done[value] = false;
            }
        } else if (pageName == 'question') {
            var request = $.ajax({
                url:"/rest/prolog/reasoning/question/next",
                type:"GET",
                success:function() {
                    var questionId = request.responseText;
                    if (request.responseText == 'none') {
                        $('#content').html('Hệ thống đã thực hiện hỏi xong, bấm sang trang kết quả để xem.');
                    } else if (done[questionId] == true) {
                        loadPage('question');
                    }
                    else {
                        var request2 = $.ajax({
                            url:"/rest/prolog/questions/" + questionId,
                            type:"GET",
                            success:function() {
                                var question = eval('(' + request2.responseText + ')');
                                var questionHtml = question.question + '<br/>';
                                for (var i = 0; i < question.answers.length; i++) {
                                    questionHtml += '<input type="checkbox" name="' + questionId + '" value="' + question.answers[i].text + '">' + question.answers[i].text + '<br/>';
                                }
                                questionHtml += '<input type="button" value="Trả lời" onClick="answer(\'' + questionId + '\')">';
                                $('#content').html(questionHtml);
                            }
                        });
                    }
                }
            });
        } else if (pageName == 'result') {
            var request = $.ajax({
                url:"/rest/prolog/reasoning/laptops/all",
                type:"GET",
                success:function() {
                    var resultHtml = "";
                    var laptops = eval('(' + request.responseText + ')');
                    if (laptops.length) {
                        for (var i = 0; i < laptops.length; i++) {
                            var laptop = laptops[i];
                            resultHtml += (i + 1) + ':' + laptop.name + '<br/>';
                            resultHtml += '<a href="' + laptop.url + '">Website</a><br/>';
                            resultHtml += 'với các thuộc tính:';
                            resultHtml += laptop.refinedAttribute[0];
                            for (var j = 1; j < laptop.refinedAttribute.length; j++) {
                                resultHtml += ', ' + laptop.refinedAttribute[j];
                            }
                            resultHtml += '<br/><hr/>'
                            $('#content').html(resultHtml);
                        }
                    }
                }
            });
        }
    }
}

var answer = function(questionId) {
    var elements = $('[name=' + questionId + ']');
    done[questionId] = true;
    var answer = "";
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].checked) {
            answer += "----" + elements[i].value;
        }
    }
    pingUrl('/rest/prolog/reasoning/answer/' + questionId + '/' + answer);
    loadPage('question');
}


var pingUrl = function(url) {
    var request = $.ajax({
        url:url,
        type:"GET",
        success:function() {

        }
    });
}